package com.example.pc.day_10_8;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private String TAG = MyReceiver.class.getSimpleName();
    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //第二步:注册广播(动态注册)
        //创建需要注册的广播对象
        myReceiver = new MyReceiver();
        //创建意图过滤器,指定广播的Action(频道)
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.setPriority(Integer.MAX_VALUE);
        intentFilter.addAction("a.b.c.d");//指定Action
        intentFilter.addAction("a.b.c.d1");//指定Action
        intentFilter.addAction("a.b.c.d2");//指定Action
        intentFilter.addAction("a.b.c.d3");//指定Action
        intentFilter.addAction("a.b.c.d4");//指定Action
        //注册广播
        registerReceiver(myReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myReceiver!=null){
            /**第四步:注销广播,在不需要广播接受者的时候注销广播*/
            unregisterReceiver(myReceiver);
        }
    }
//public class MyReceiver extends BroadcastReceiver{}定义一个广播接受者的类
//    registerReceiver(注册)
//    sendBroadcast(发送)
//    unregisterReceiver(注销)
    /**
     * 点击按钮发送广播消息
     * @param view
     */
    public void send(View view) {

        //第三步:发送广播消息
        Intent intent = new Intent("a.b.c.d");

        intent.putExtra("s","ooooo");
        intent.putExtra("i",3333333);
//        sendBroadcast(intent);//无序广播
        sendBroadcast(intent,null);//有序广播
        Log.e(TAG, "sendBR: 发送了广播");

    }
}
